package com.practice.order.controller;


import com.practice.order.order.bean.Order;
import com.practice.order.properties.OrderProperties;
import com.practice.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
//@RefreshScope
@Slf4j
//@RequestMapping("/api/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

/*    @Value("${order.timeout}")
    String orderTimeOut;

    @Value("${order.auto-comfirm}")
    String orderAutoConfirm;*/
    @Autowired
    private OrderProperties orderProperties;

    //创建订单
    @GetMapping("/create")
    public Order createOrder(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId){
        Order order = orderService.createOrder( productId ,  userId);
        return order;
    }

    //这个接口主要是实现：sentinel的流控模式--链路策略的测试
    //创建秒杀订单
    @GetMapping("/seckill")
    public Order seckill(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId){

        Order order = orderService.createOrder(productId, userId);
        order.setId(Long.MAX_VALUE);
        return order;
    }

    /**
     * 测试 流控模式 - 读取 数据
     * @return
     */
    @GetMapping("/writeDb")
    public String writeDb(){
        log.info("writeDb.....");
        return "writeDb success.....";
    }

    /**
     * 测试 流控模式 - 写入 数据
     * @return
     */
    @GetMapping("/readDb")
    public String readDb(){
        log.info("readDb.....");
        return "readDb success.....";
    }

    //获取Nacos配置好的值
    @GetMapping("/getNacosConfig")
    public String getNacosConfig(){
        System.out.println("order.timeout:"+orderProperties.getTimeOut()+" order.auto-comfirm:"+orderProperties.getAutoComfirm());
        return "order.timeout:"+orderProperties.getTimeOut()+" order.auto-comfirm:"+orderProperties.getAutoComfirm();
    }

}
